package ca.etsmtl.log430.lab3;
import java.io.PipedReader;
import java.io.PipedWriter;
//import java.util.regex.Pattern;
//import java.util.regex.Matcher;
public class FormatFilter2 extends Thread{

	// Declarations

	PipedReader inputPipe = new PipedReader();
	PipedWriter outputPipe = new PipedWriter();
	String currentState;

	public FormatFilter2(PipedWriter inputPipe,PipedWriter outputPipe, String currentState) {
		
		this.currentState = currentState;
		try {
			// Connect InputPipes to upstream filters
			this.inputPipe.connect(inputPipe);
			System.out.println("FormatFilter:: connected to upstream filters."+ inputPipe);
		} catch (Exception Error) {
			System.out.println("FormatFilter:: Error connecting input pipes.");
		} // try/catch

		try {
			// Connect outputPipe to downstream filter
			this.outputPipe = outputPipe;
			System.out.println("FormatFilter:: connected to downstream filter.");
		} catch (Exception Error) {
			System.out.println("FormatFilter:: Error connecting output pipe.");
		} // catch

	} // Constructor

	// This is the method that is called when the thread is started

	public void run() {
		// Declarations
		
		boolean done;

		// Begin process data from the input pipes
		try {
			// Declarations

			// Need to be an array for easy conversion to string
			char[] characterValue = new char[1];
			// Indicate when you are done reading on pipe
			done = false;
			// integer values read from the pipes
			int integerCharacter;
			// lines of text from input pipe
			String lineOfText1 = "";
			// Indicate whether lines of text are ready to be output
			// to downstream filters
			boolean write1 = false;

			// Loop for reading data

			while (!done) {
				// Read pipe 
				if (!done) {
					integerCharacter = inputPipe.read();
					
					characterValue[0] = (char) integerCharacter;
					//System.out.println("yahoooooooooooooooooooou:"+(char) integerCharacter);
					if (integerCharacter == -1) // pipe #1 is closed
					{
						done = true;
						System.out.println("FormatFilter:: Input pipe closed.");

						try {
							inputPipe.close();
						} catch (Exception Error) {
							System.out.println("FormatFilter:: Error closing input pipe 1.");
						} // try/catch

					} else {

						if (integerCharacter == '\n') // end of line
						{
							System.out.println("FormatFilter:: Received: "
									+ lineOfText1 + " on input pipe .");
							lineOfText1=Formater(lineOfText1);					
							write1 = true;
						} else {
							lineOfText1 += characterValue[0];
						} // if

					} // if ( IntegerCharacter1 == -1 )
					
				} // if (!Done1)
				
				

				if (write1) {
					write1 = false;
					
					try {
						System.out.println("FormatFilter:: Sending "
								+ lineOfText1 + " to output pipe.");
						lineOfText1 += "\n";
						outputPipe.write(lineOfText1, 0, lineOfText1.length());
						outputPipe.flush();
					} catch (Exception IOError) {
						System.out.println("FormatFilter:: Write Error.");
					} // try/catch

					lineOfText1 = "";

				} // if ( Write1 )

				
			} // while ( !Done )

		} // try

		catch (Exception Error) {
			System.out.println("FormatFilter:: Interrupted.");
		} // catch

		try {
			System.out.println("FormatFilter:: output pipe closed.");
			outputPipe.close();
		} catch (Exception Error) {
			System.out.println("FormatFilter:: Error closing pipes");
		} // try/catch

	} // run
	/**
	 * Cette methode change le format des donnees de sortie de telle sorte qu elles ne comprennent que 
	 * les champs suivants : statut, etat, taux, num de projet
	 * @param lineOfText1
	 * @return
	 */
	private String Formater(String lineOfText1) {
			String newLine= "";
			
			String[] tab=lineOfText1.split(" ");
			
			String statut=tab[1];
			newLine+=statut+" ";
			
			String etat=tab[5];
			newLine+=etat+" ";
			
			String taux=tab[4];
			newLine+=taux+" ";
			
			String numProject=tab[0];
			newLine+=numProject;
			
		return newLine;
	}

} // class